package com.pm.service.official.impl;

import com.pm.entity.po.official.OfficialComRank;
import com.pm.entity.po.official.OfficialParamPo;
import com.pm.entity.vo.official.OfficialCommonVo;
import com.pm.mapper.official.OfficialComRankMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.official.OfficialBaseDataService;
import com.pm.service.official.OfficialComRankService;
import com.pm.service.official.OfficialMeanDevRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: pm_info_api
 * @description:党政一把手综合排名
 * @author: ht
 * @create: 2020-08-26 17:46
 **/
@Service
public class OfficialComRankServiceImpl extends BaseServiceImpl<OfficialComRank> implements OfficialComRankService {

    @Autowired
    OfficialComRankMapper officialComRankMapper;

    @Autowired
    OfficialMeanDevRankService officialMeanDevRankService;

    @Autowired
    OfficialBaseDataService officialBaseDataService;

    @Resource
    private void setBaseMapper(OfficialComRankMapper officialComRankMapper) {
        super.setBaseMapper(officialComRankMapper);
    }

    @Override
    public Integer addOne(OfficialComRank officialComRank) {
        if (officialComRank.getId() != null) {
            officialComRankMapper.removeOne(officialComRank.getId());
            return officialComRankMapper.addOne(officialComRank);
        }
        return officialComRankMapper.addOne(officialComRank);
    }

    @Override
    public List<OfficialComRank> getList(OfficialCommonVo officialCommonVo) {
        return officialComRankMapper.getList(officialCommonVo);
    }

    // keyList value组成：任务id_年份_上下半年
    @Override
    public void batchChange(List<String> keyList) {
        String taskId = null;
        String year = null;
        String timeNode = null;
        OfficialComRank officialComRank = null;
        OfficialParamPo officialParamPo = new OfficialParamPo();
        // 总得分
        Double scoreSum = 0D;
        for (String key : keyList) {
            String[] keys = key.split("_");
            taskId = keys[0];
            year = keys[1];
            timeNode = keys[2];
            officialParamPo.setTimeNode(Integer.valueOf(timeNode));
            officialParamPo.setYear(year);
            // 查询xx年xx半年有多少人参与了评分
            int userNum = officialMeanDevRankService.getUserNumByYearAndTimeNode(year, timeNode);
            officialParamPo.setUserNum(userNum);
            // 总分
            scoreSum = officialBaseDataService.getScoreSumByTaskIdAndTimeNode(taskId, timeNode);
            officialParamPo.setScoreSum(scoreSum);

            officialComRank = officialComRankMapper.getComRankByTaskIdAndTimeNode(taskId, timeNode);
            List<OfficialComRank> insertList = new ArrayList<>();
            List<OfficialComRank> updateList = new ArrayList<>();
            if (officialComRank == null) {
                officialComRank = new OfficialComRank();
            }
            officialComRank.setTimeNode(Integer.valueOf(timeNode));
            officialComRank.setTaskId(Integer.valueOf(taskId));
            officialComRank.setScoreSum(scoreSum);
            if (officialComRank.getId() == null) {
                insertList.add(officialComRank);
            } else {
                updateList.add(officialComRank);
            }
            if (!insertList.isEmpty()) {
                officialComRankMapper.batchAdd(insertList);
            }
            if (!updateList.isEmpty()) {
                officialComRankMapper.batchUpdate(updateList);
            }

        }
        // 根据总分，年份，上下半年，修改已经存在并且比当前总分高的排名
        updateRankByScoreSum(officialParamPo);
        // 根据总分，年份，上下半年，修改平均分
        officialComRankMapper.updateScoreAvg(officialParamPo);
        // 设定离均差总分和排名
        officialMeanDevRankService.updateRankByMeanDevSum(officialParamPo);
    }

    /**
     * 修改排序
     *
     * @param officialParamPo
     * @return
     */
    private int updateRankByScoreSum(OfficialParamPo officialParamPo) {
        List<OfficialComRank> officialComRankList = officialComRankMapper.getRankSortList(officialParamPo);
        int rank = 0, index = 0;
        OfficialComRank officialComRank = null;
        for (int i = 0, j = officialComRankList.size(); i < j; i++) {
            index++;
            officialComRank = officialComRankList.get(i);
            if (i == 0) {
                rank++;
            } else if (officialComRank.getScoreSum() - officialComRankList.get(i - 1).getScoreSum() == 0) {
                rank = officialComRankList.get(i - 1).getRank();
            } else {
                rank = index;
            }
            officialComRank.setRank(rank);
            officialComRankList.set(i, officialComRank);
        }
        return officialComRankMapper.batchUpdate(officialComRankList);
    }
}
